#!/usr/bin/env python

from liwe.module import LiweModule
from mmh import MediaManagerHandler
import os

class MediaManager ( LiweModule ):
	def __init__ ( self, liwe ):
		super ( MediaManager, self ).__init__ ( liwe, "media_manager" )
		self.mmh = MediaManagerHandler ( self.dbm, self.cfg )
		self.mmh.path = os.path.join ( ".", "site", "media_manager" )

	def add_image ( self, module, id_obj, descr, file_obj, pos = 0, ext = '' ):
		return self.mmh.add_image ( module, id_obj, descr, file_obj, pos )

	def add_flash ( self, module, id_obj, descr, file_obj, pos = 0, ext = '' ):
		return self.mmh.add_flash ( module, id_obj, descr, file_obj, pos )

	def add_file ( self, module, id_obj, descr, fname, pos = 0, ext = '', kind = 'file' ):
		return self.mmh.add_file ( module, id_obj, descr, fname, pos, ext, kind )

	def add_url ( self, module, id_obj, descr, url, pos = 0 ):
		return self.mmh.add_url ( module, id_obj, descr, url, pos )


	def add_youtube ( self, module, id_obj, descr, youtube_url, pos = 0 ):
		id_media = self.mmh.add_youtube ( module, id_obj, descr, youtube_url, pos ) 
		return id_media

	def del_media ( self, id_media ):
		module, id_obj = self.mmh.del_media ( id_media )
		order_mode = ''
		#sys.stderr.write ( "**** ID_MEDIA: %s - ID_OBJ: %s" % ( id_media, id_obj ) )
		return self.mmh.get_items ( module, id_obj, order_mode )

	def get_items ( self, module, id_obj, order_mode = '' ):
		return self.mmh.get_items ( module, id_obj, order_mode )

	def del_items ( self, module, id_obj ):
		return self.mmh.del_items ( module, id_obj )

	def edit ( self, id_media, descr, pos, data ):
		dct = { "descr" : descr, "pos" : pos }
		if data: 
			dct [ "data" ] = data

		self.dbm.update ( "media_data", dct, "id_media = '%s'" % id_media )

	def rotate ( self, id_media, direction ):
		return self.mmh.rotate ( id_media, direction )

	def change_pos ( self, id_media, pos ):
		self.dbm.update ( "media_data", { "pos" : pos }, "id_media = '%s'" % id_media )

	def serve_file ( self, kind, mode, id_media, ext ):
		return self.mmh.serve_file ( kind, mode, id_media, ext )

